/**
 * 
 */
package rentReturnLogic;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;



import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import rentReturnEntity.RentEntity;
import rentReturnService.RentService;
import reservationEntity.ReservationEntity;
import vehicleEntity.AvailableVehicleDisplayable;

/**
 * @author Aakash Singh
 *
 */
public class RentLogic {

/**
 * 
 * @return
 * @throws SQLException
 */
	public ReservationEntity fetchReservationDetails() throws SQLException{
		
		return null;
	}
	private static RentService rentService = null;
	
	
	public ObservableList<AvailableVehicleDisplayable> fetchOverdueVehicles () throws SQLException
	{
		rentService = new RentService();
		ObservableList<AvailableVehicleDisplayable> availableVehicleList= FXCollections.observableArrayList();
		
		ResultSet rentResultSet=null;
		try {
			rentResultSet = rentService.fetchOverdueVehicle();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			throw e1;
		}
		if(rentResultSet != null){
			try {
				while(rentResultSet.next()){
					System.out.println(rentResultSet.getInt(1));
					AvailableVehicleDisplayable availVehilceforRent = new AvailableVehicleDisplayable(rentResultSet.getInt(1),rentResultSet.getString(4),rentResultSet.getString(5),rentResultSet.getString(6),rentResultSet.getDate(9).toString()); 
					availableVehicleList.add(availVehilceforRent);
					System.out.println("Vehicle "+availVehilceforRent.getVehicleId());
					/** 
					 * Current Odometer attribute missing in vehicle table
					 */
				}
				System.out.println("Total in service "+availableVehicleList.size());
				rentService.closeConnection();
				return availableVehicleList;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw e;
			}
		}else{
			System.err.println("No veh rec found ! ");
		}
		
		rentService.closeConnection();
		return null;
		
	}
	
	/**
	 * This method will call the corresponding method from the service class to fetch the vehicles from
	 * the database corresponding to the reserved Vehicle Type
	 * @param reservedVehicleType
	 * @return
	 * @throws SQLException
	 */
	public ObservableList<AvailableVehicleDisplayable> fetchVehicle(String reservedVehicleType) throws SQLException{
		
		rentService = new RentService();
		ObservableList<AvailableVehicleDisplayable> availableVehicleList= FXCollections.observableArrayList();
		
		
		ResultSet rentResultSet=null;
		try {
			rentResultSet = rentService.fetchVehicle(reservedVehicleType);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			throw e1;
		}
		if(rentResultSet != null){
			try {
				while(rentResultSet.next()){
					AvailableVehicleDisplayable availVehilceforRent = new AvailableVehicleDisplayable(rentResultSet.getInt(1),rentResultSet.getString(2),rentResultSet.getString(3),rentResultSet.getString(4)); 
					availableVehicleList.add(availVehilceforRent);
					
					/** 
					 * Current Odometer attribute missing in vehicle table
					 */
				}
				rentService.closeConnection();
				return availableVehicleList;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw e;
			}
		}else{
			System.err.println("No veh rec found ! ");
		}
		
		rentService.closeConnection();
		return null;
	}
	
	/**
	 * This method saves the rent details, returns the rent ID 
	 * @param rent
	 * @return
	 */
	public int saveRent(RentEntity rent,LocalDate returnDate) throws SQLException{
		RentService rs = new RentService();
		ResultSet res=rs.saveRent(rent,returnDate);
		try {
			if(res.next())
			return res.getInt(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return 0;
	}

	/**
	 * @param phoneNumber
	 * @throws SQLException 
	 */
	public String fetchUserName(String phoneNumber) {
		RentService rs = new RentService();
		try {
			ResultSet res=rs.fetchUserName(phoneNumber);
			if(res.next())
			return res.getString("name");
			else{
				return "";
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return "";
		}
		
		
	}
}
